Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

write a program to create stack class and implement all its methods (use lists) in python

write a program to create stack class and implement all its methods (use lists) in python
2 min read

 

What is a Stack?

A stack is a linear data structure where data is arranged objects on over another. It stores the data in LIFO (Last in First Out) manner. The data is stored in a similar order as plates are arranged one above another in the kitchen. The simple example of a stack is the Undo feature in the editor. The Undo feature works on the last event that we have done.

We always pick the last plate from the stack of the plate. In stack, the new element is inserted at the one end and an element can be removed only that end.

We can perform the two operations in the stack - PUSH and POP. The PUSH operation is when we add an element and the POP operation is when we remove an element from the stack.

Methods of Stack

Python provides the following methods that are commonly used with the stack.

  • empty() - It returns true, it the stack is empty. The time complexity is O(1).
  • size() - It returns the length of the stack. The time complexity is O(1).
  • top() - This method returns an address of the last element of the stack. The time complexity is O(1).
  • push(g) - This method adds the element 'g' at the end of the stack - The time complexity is O(1).
  • pop() - This method removes the topmost element of the stack. The time complexity is O(1).

Implementation Using List

Python list can be used as the stack. It uses the append() method to insert elements to the list where stack uses the push() method. The list also provides the pop() method to remove the last element, but there are shortcomings in the list. The list becomes slow as it grows.

The list stores the new element in the next to other. If the list grows and out of a block of memory then Python allocates some memory. That's why the list become slow. Let's understand the following example -

  1. # initial empty stack  
  2. my_stack = []  
  3.   
  4. # append() function to push   
  5. # element in the my_stack   
  6. my_stack.append('x')  
  7. my_stack.append('y')  
  8. my_stack.append('z')  
  9.   
  10.   
  11. print(my_stack)  
  12.   
  13. # pop() function to pop   
  14. # element from my_stack in    
  15. # LIFO order   
  16. print('\nElements poped from my_stack:')  
  17. print(my_stack.pop())  
  18. print(my_stack.pop())  
  19. print(my_stack.pop())  
  20.   
  21. print('\nmy_stack after elements are poped:')  
  22. print(my_stack)   

Output:

['x', 'y', 'z']

Elements poped from my_stack:
z
y
x  

my_stack after elements are poped:
[]
Traceback (most recent call last):
  File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/Stack.py", line 21, in <module>
    print(my_stack.pop())
IndexError: pop from empty list

Explanation -

In the above code, we have defined an empty list. We inserted the elements one by one using the append() method. That is similar to the push() method. We also removed the elements using the pop() method. The pop() method returns the last element of the list.

You may like these posts

Post a Comment

© 2025Python . The Best Codder All rights reserved. Distributed by